import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class DialogHelper {
  // 显示通用弹窗
  static Future<void> showCustomDialog({
    required BuildContext context,
    String? title,
    String? content,
    List<Widget>? actions,
    Widget? customContent,
    Color? backgroundColor,
    TextStyle? titleStyle,
    TextStyle? contentStyle,
  }) async {
    return showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          backgroundColor: backgroundColor,
          title: title != null
              ? Text(
                  title!,
                  style:
                      titleStyle ?? Theme.of(context).textTheme.headlineMedium,
                )
              : null,
          content: customContent ??
              (content != null
                  ? Text(
                      content,
                      style: contentStyle ??
                          Theme.of(context).textTheme.bodyMedium,
                    )
                  : null),
          actions: actions,
        );
      },
    );
  }

  // 显示确认弹框
  static Future<bool?> showConfirmDialog({
    required BuildContext context,
    String? title,
    String? content,
    String confirmText = '确认',
    String cancelText = '取消',
    VoidCallback? confirmCall,
  }) async {
    return showDialog<bool>(
      context: context,
      builder: (context) {
        return AlertDialog(
          shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(10.r)),
          titlePadding: const EdgeInsets.fromLTRB(15, 15, 15, 0),
          contentPadding: const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
          insetPadding: const EdgeInsets.all(20),
          buttonPadding: EdgeInsets.zero,
          actionsPadding: EdgeInsets.zero,
          title: title != null ? Text(title) : null,
          content: content != null ? Text(content) : null,
          actions: [
            TextButton(
              onPressed: () => Navigator.of(context).pop(false),
              child: Text(cancelText),
              style: ButtonStyle(
                  backgroundColor: MaterialStateColor.resolveWith(
                      (states) => Colors.transparent)),
            ),
            TextButton(
              onPressed: () {
                Navigator.of(context).pop(true);
                confirmCall!();
              },
              child: Text(confirmText),
              style: ButtonStyle(
                  backgroundColor: MaterialStateColor.resolveWith(
                          (states) => Colors.transparent)),
            ),
          ],
        );
      },
    );
  }

  // 显示输入弹框
  static Future<String?> showInputDialog({
    required BuildContext context,
    String? title,
    String? hintText,
    String confirmText = '确认',
    String cancelText = '取消',
  }) async {
    final TextEditingController controller = TextEditingController();
    return showDialog<String>(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: title != null ? Text(title) : null,
          content: TextField(
            controller: controller,
            decoration: InputDecoration(hintText: hintText),
          ),
          actions: [
            TextButton(
              onPressed: () => Navigator.of(context).pop(),
              child: Text(cancelText),
            ),
            TextButton(
              onPressed: () => Navigator.of(context).pop(controller.text),
              child: Text(confirmText),
            ),
          ],
        );
      },
    );
  }

  // 显示选择弹框
  static Future<void> showSelectionDialog({
    required BuildContext context,
    String? title,
    required List<String> options,
  }) async {
    return showDialog(
      context: context,
      builder: (context) {
        return SimpleDialog(
          title: title != null ? Text(title) : null,
          children: options.map((option) {
            return SimpleDialogOption(
              onPressed: () {
                Navigator.of(context).pop(option);
              },
              child: Text(option),
            );
          }).toList(),
        );
      },
    );
  }

  // 显示toast提示消息
  static Future<void> showToast({
    required BuildContext context,
    String? message,
  }) async {
    return showDialog(
      context: context,
      barrierDismissible: false, // 点击外部不可关闭
      builder: (context) {
        return AlertDialog(
          content: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              //CircularProgressIndicator(),
              SizedBox(height: 16),
              Text(message ?? '加载中...'),
            ],
          ),
        );
      },
    );
  }

  // 显示加载弹框
  static Future<void> showLoadingDialog({
    required BuildContext context,
    String? message,
  }) async {
    return showDialog(
      context: context,
      barrierDismissible: false, // 点击外部不可关闭
      builder: (context) {
        return AlertDialog(
          content: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              CircularProgressIndicator(),
              SizedBox(height: 16),
              Text(message ?? '加载中...'),
            ],
          ),
        );
      },
    );
  }

  // 显示底部弹框
  static Future<void> showBottomSheetDialog({
    required BuildContext context,
    required Widget child,
  }) async {
    return showModalBottomSheet(
      context: context,
      builder: (context) {
        return child;
      },
    );
  }
}
